package com.example.mybaseapp.ui.floatingview

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.localbroadcastmanager.content.LocalBroadcastManager
import com.example.lib_base.BaseActivity
import com.example.mybaseapp.R
import com.example.mybaseapp.databinding.ActivityFloatintViewBinding
import com.google.android.material.internal.ViewUtils.dpToPx

class FloatintViewActivity : BaseActivity<ActivityFloatintViewBinding>() {
    private lateinit var localBroadcastManager:LocalBroadcastManager
    private lateinit var localBroadcastReceiver: FloatingLocalBroadcastReceiver


    override fun onViewInit() {
        localBroadcastManager = LocalBroadcastManager.getInstance(this)
        localBroadcastReceiver = FloatingLocalBroadcastReceiver()
        localBroadcastManager.registerReceiver(localBroadcastReceiver, IntentFilter("com.example.floating.localreceiver"))
        binding.btnStartFloating.setOnClickListener {
            // Check if the permission is granted
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!Settings.canDrawOverlays(this)) {
                    // Request permission
                    val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
                    startActivity(intent)
                } else {
                    startFloatingWindowService()
                }
            } else {
                startFloatingWindowService()
            }
        }
        binding.btnStartFloatingFree.setOnClickListener {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (!Settings.canDrawOverlays(this)) {
                    // Request permission
                    val intent = Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION)
                    startActivity(intent)
                } else {
                    startFloatingFreeWindowService()
                }
            } else {
                startFloatingFreeWindowService()
            }
        }
    }

    private fun startFloatingFreeWindowService() {
        val serviceIntent = Intent(this, FloatingFreeWindowService::class.java)
        startService(serviceIntent)
        moveTaskToBack(true)
    }

    private fun startFloatingWindowService() {
        val serviceIntent = Intent(this, FloatingWindowService::class.java)
        startService(serviceIntent)
        moveTaskToBack(true)
    }

   inner class FloatingLocalBroadcastReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context?, intent: Intent?) {
            // 处理接收到的本地广播
            Toast.makeText(context, "本地广播", Toast.LENGTH_SHORT).show()
            val activityIntent = Intent(context, FloatintViewActivity::class.java)
            activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
            context!!.startActivity(activityIntent)
        }
    }
}
